{layout name="public/layout" /}
{include file='yxnavbar'}
<style>
    .div_flex{
        display: flex !important;
        z-index: 99;
    }
    .div_none{
        display: none; !important;
    }
    .shadow-box{
        width: 100%;
        height: 100%;
        position: fixed;
        top: 0;
        left: 0;
        background: rgba(0,0,0,0.5);
    }
    .alert_form {
        position: absolute;
        top: 10%;
        left: 30%;
        background: #fff;
        height: 80vh;
        width: 40vw;
        max-height: 70vh;
    }
    .shadow-box .panel-body{
        max-height: 90%;
        overflow-y: auto;
    }
    .shadow-box .panel .panel-heading .btn{
    padding-bottom: 2px;
        padding-top: 0px;
}
</style>



<div class="main">
    {include file='../../plugin/coupon/template/web/common_nav'}
    <div class="panel panel-info">
        <div class="panel-heading">当前数量： {$total}</div>
        <div class="panel-body">
            <form id = "myform" action="./index.php" method="get" class="form-horizontal" role="form">
                <input type="hidden" name="c" value="site" />
                <input type="hidden" name="a" value="entry" />
                <input type="hidden" name="m" value="{$_P['m']}" />
                <input type="hidden" name="do" value="web" />
                <input type="hidden" name="r" value="coupon" />

                <div class="form-group">
                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">优惠券状态</label>
                    <div class="col-xs-11 col-sm-9 col-md-9">
                        <div class="btn-group">
                            {loop $status_arr $row}
                            <a href="{php echo createWebUrlPlugin('coupon',array('themeid'=>$themeid,'status'=>$row['value']));}"
                               class="btn {if $status == $row['value']}btn-primary{else}btn-default{/if}">
                                {$row['title']}
                                <span style="color: #ff0000">{$row['total']}</span>
                            </a>
                            {/loop}
                        </div>
                    </div>
                    {if count($themelist)>1 }
                    <div class="col-xs-12 col-sm-2 col-md-2">
                        <select onchange="submitForm();" class="form-control" name="themeid">
                            {loop $themelist $row}
                            <option value="{$row['id']}" {if $themeid == $row['id'] }selected="selected"{/if}>{$row['id']} - {php echo mb_substr($row['title'],0,8,'utf-8');}</option>
                            {/loop}
                        </select>
                    </div>
                    {else}
                    <input type="hidden" name="themeid" value="{$themeid}" />
                    {/if}
                </div>
                {if $is_presale}
                <!--                <div class="form-group">-->
                <!--                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">领取身份</label>-->
                <!--                    <div class="col-xs-11 col-sm-9 col-md-9">-->
                <!--                        <div class="btn-group">-->
                <!--                            {loop $target_arr $row}-->
                <!--                            <a href="{php echo createWebUrlPlugin('coupon',array('themeid'=>$themeid,'target'=>$row['value']));}"-->
                <!--                               class="btn {if $target == $row['value']}btn-primary{else}btn-default{/if}">-->
                <!--                                {$row['title']}-->
                <!--                                <span style="color: #ff0000">{$row['total']}</span>-->
                <!--                            </a>-->
                <!--                            {/loop}-->
                <!--                        </div>-->

                <!--                    </div>-->

                <!--                </div>-->
                <div class="form-group">
                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">领取方式</label>
                    <div class="col-xs-11 col-sm-9 col-md-9">
                        <div class="btn-group">
                            {loop $get_type_arr $row}
                            <a href="{php echo createWebUrlPlugin('coupon',array('themeid'=>$themeid,'get_type'=>$row['value']));}"
                               class="btn {if $get_type == $row['value']}btn-primary{else}btn-default{/if}">
                                {$row['title']}
                                <span style="color: #ff0000">{$row['total']}</span>
                            </a>
                            {/loop}
                        </div>
                    </div>
                </div>
                {/if}
                <!--                <div class="form-group">-->
                <!--                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">使用时间</label>-->
                <!--                    <div class="col-xs-11 col-sm-9 col-md-9">-->
                <!--                        <div class="btn-group">-->
                <!--                            {loop $show_type_arr $row}-->
                <!--                            <a href="{php echo createWebUrlPlugin('coupon',array('themeid'=>$themeid,'show_type'=>$row['value']));}"-->
                <!--                               class="btn {if $show_type == $row['value']}btn-primary{else}btn-default{/if}">-->
                <!--                                {$row['title']}-->
                <!--                                <span style="color: #ff0000">{$row['total']}</span>-->
                <!--                            </a>-->
                <!--                            {/loop}-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                </div>-->
                {if $is_presale}
                <div class="form-group">
                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">使用条件</label>
                    <div class="col-xs-11 col-sm-9 col-md-9">
                        <div class="btn-group">
                            {loop $used_type_arr $row}
                            <a href="{php echo createWebUrlPlugin('coupon',array('themeid'=>$themeid,'used_type'=>$row['value']));}"
                               class="btn {if $used_type == $row['value']}btn-primary{else}btn-default{/if}">
                                {$row['title']}
                                <span style="color: #ff0000">{$row['total']}</span>
                            </a>
                            {/loop}
                        </div>
                    </div>
                </div>
                {/if}
<!--                <div class="form-group">-->
<!--                    <label class="col-xs-12 col-sm-3 col-md-1 control-label">创建时间</label>-->
<!--                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">-->
<!--                        {php echo view_form_field_daterange('createtime', array('start'=>date('Y-m-d H:i:s',$starttime),'end'=>date('Y-m-d H:i:s',$endtime)), true);}-->
<!--                    </div>-->

<!--                </div>-->
                <div class="form-group clearfix">
                    <label class="col-xs-12 col-sm-3 col-md-1 control-label">关键字</label>
                    <div class="col-sm-3 col-xs-3 col-md-3">
                        <input class="form-control" name="search_data" placeholder="输入优惠券名称" type="text" value="{$_P['search_data']}">
                    </div>
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        <button class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
                        <button class="btn btn-primary" onclick="jumpAdd();" type="button"><i class="fa fa-plus-square"></i> 添加优惠券</button>
                        <input type="hidden" name="__token__" value="{$Request.token}"/>
                    </div>

                </div>
                <span class="help-block" style="color: red;">100元内减免金额支持小数点后1位，100元以上减免金额仅支持整数</span>
            </form>
        </div>
    </div>
    <div class="tab-content">
        <div class="panel panel-default">
            <div class="panel-body table-responsive">
                <table class="table table-hover">
                    <thead class="navbar-inner">
                    <tr>
                        <th style="width: 50px">序号</th>
                        <th style="width: 120px">优惠券名称</th>
                        <th style="width: 140px">优惠券金额</th>
                        <th style="width: 80px">已领取</th>
                        <th style="width: 80px">已使用</th>
                        <th style="width: 80px">库存</th>

                        {if $is_presale}
<!--                        <th style="width: 100px">领取身份</th>-->
                        <th style="width: 80px">领取方式</th>
                        {/if}
<!--                        <th style="width: 100px">领取时间</th>-->
                        <th style="width: 100px">使用时间</th>
                        {if $is_presale}
                        <th style="width: 80px">使用条件</th>
                        {/if}
                        <th style="width: 60px">状态</th>
                        <th style="width: 100px">创建时间</th>
                        <th style="width: 100px;text-align:right;">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {loop $list $index $item}
                    <tr>
                        <td>{$item['id']}</td>
                        <td>{$item['name']}</td>
                        <td>
                            {if $item['price_info']}
                            <?php $item_price_info = json_decode($item['price_info'],true);?>
                            {loop $item_price_info $row}
                            {if $row['price']}
                                {if $config['coupon_use_type']==3}
                                满{$row['limit']}元，减免{$row['price']}元<br/>
                                {else}
                                减免{$item['price']}{if $item['price'] && $item['max_price'] && $item['price']!=$item['max_price']}~{$item['max_price']}{/if}元<br/>
                                {/if}
                            {/if}
                            {/loop}
                            {else}
                            减免{$item['price']}{if $item['price'] && $item['max_price'] && $item['price']!=$item['max_price']}~{$item['max_price']}{/if}元
                            {/if}
                        </td>
                        <td>
                            <span class="label label-success"> <a href="{php echo createWebUrlPlugin('coupon.flag',array('themeid'=>$themeid,'coupon_id'=>$item['id']));}">{$item['get_total']}</a></span>
                        </td>
                        <td>
                            <span class="label label-success"> <a href="{php echo createWebUrlPlugin('coupon.flag',array('themeid'=>$themeid,'coupon_id'=>$item['id']));}">{$item['use_total']}</a></span>
                        </td>
                        <td>

                            {$item['qty']}
                        </td>
                        {if $is_presale}
                        <td>

                                {loop $select_get_type_arr $row}
                                    {if $item['get_type']==$row['value']}
                                        {$row['title']}
                                    {/if}
                                {/loop}
                        </td>
                        {/if}
<!--                        <td>-->
<!--                            {if $item['get_status']==1}-->
<!--                                <span class="label label-success">不限</span>-->
<!--                            {else}-->
<!--                                {$item['get_start_time']}<br/>{$item['get_end_time']}<br/>-->
<!--                            {/if}-->
<!--                        </td>-->
                        <td>
                                {loop $select_show_type_arr $row}
                                    {if $item['show_type'] == $row['value']}
                                        {$row['title']}
                                    {/if}
                                {/loop}
                            {if $item['show_type']==2}<br/>{$item['used_start_time']}<br/>{$item['used_end_time']}<br/>{/if}
                        </td>
                        {if $is_presale}
                        <td>
                                {loop $select_used_type_arr $row}
                                    {if $item['used_type']==$row['value']}
                                        {$row['title']}
                                    {/if}
                                {/loop}
                        </td>
                        {/if}
                        <td>
                            {if $item['status']==1}
                            启用
                            {elseif $item['status']==2}
                            禁用
                            {else}
                            过期
                            {/if}
                        </td>
                        <td>
                            {$item['created_at']}
                        </td>
                        <td class="str_center" style="text-align:right;">
                            <a class="btn btn-warning btn-sm" href="{php echo createWebUrlPlugin('coupon.edit',array('themeid'=>$themeid,'id'=>$item['id']));}" data-id="{$item['id']}"  title="编辑"><i class="icon-edit"></i>编辑</a>
                                                    {if $item['can_del']}
                                                    <a onclick="changeOrDelItem(this)"
                                                       data-edit-url="{php echo createWebUrlPlugin('coupon.delItem');}" data-action="del"
                                                       data-id="{$item['id']}" data-themeid="{$themeid}"  title="删除" style="width: 30px"
                                                       class="btn btn-danger btn-sm"  data-toggle="tooltip" data-placement="top">
                                                        <i class="fa fa-times"> </i></a>
                                                    {/if}
                        </td>
                    </tr>
                    {/loop}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!--新增-->
<div class="shadow-box div_none" id="shadow-box1" >
    <div class="panel panel-default alert_form" role="tabpanel" >
        <div class="panel-heading demo" >
            <span>优惠券添加</span>
            <span><input name="button"  onclick="closeAdd()"  type="button" value="关闭" class="btn btn-danger col-lg-2" style="float: right" ></span>
        </div>
        <div class="panel-body" style="z-index:188;display: flex;flex-direction: column;" >
            <form action="" method="post" class="form-horizontal" role="form" name="form" id="form1" style="padding:10px;">
                <input type="hidden" name="themeid" value="{$themeid}"/>
                <div class="form-group  ad_mq" style="display: block;">
                    <label class="col-xs-8 col-sm-4 col-md-3 col-lg-2 control-label">优惠券名称</label>
                    <div class="col-sm-8 col-xs-12">
                        <div class="input-group" style="width: 300px;">
                            <input  type="text"  class="form-control"  placeholder="请输入优惠券名称" name="name" value="">
                        </div>
                    </div>
                </div>
                {if $_C['coupon_use_type']!=3}
                <div class="form-group  ad_mq" style="display: block;">
                    <label class="col-xs-8 col-sm-4 col-md-3 col-lg-2 control-label">优惠券金额</label>
                    <div class="col-sm-8 col-xs-12">
                        <div class="input-group" style="width: 300px;">
                            <input  type="text"  class="form-control"  placeholder="请输入优惠券金额" name="price" value="">
                        </div>
                    </div>
                </div>
                {else}
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">阶梯优惠：</label>
                    <div class="col-sm-10 col-xs-12">
                        <div class="yx_coupon_items">
                            <div class="diy_coupon_prices">
                                <span class="input-group-addon diy_coupon_span">满</span>
                                <input name="yx_lt_coupon_price_limit[]" type="text" class="form-control span3 diy_coupon_input"
                                       value=""/>
                                <span class="input-group-addon diy_coupon_span">元，减免</span>
                                <input name="yx_coupon_rule_price_diff[]" type="text" class="form-control span3 diy_coupon_input"
                                       value=""/>
                                <span class="input-group-addon diy_coupon_span">元</span>
                                <input  name="yx_coupon_price_id[]" type="hidden" value="1" />
                                <a href='javascript:;' onclick='removeLtRuleItem(this)'>
                                    <i class='glyphicon glyphicon-remove'></i>
                                </a>
                            </div>
                        </div>
                        <a href="javascript:;" onclick="addLtRule();"><i class="glyphicon glyphicon-plus" title="添加优惠"></i>添加优惠</a>
                        <span class="help-block">100元内减免金额支持小数点后1位，100元以上减免金额仅支持整数</span>
                    </div>
                </div>
                {/if}
                <div class="form-group  ad_mq" style="display: block;">
                    <label class="col-xs-8 col-sm-4 col-md-3 col-lg-2 control-label">优惠券库存</label>
                    <div class="col-sm-8 col-xs-12">
                        <div class="input-group" style="width: 300px;">
                            <input  type="number"  class="form-control"   placeholder="请输入优惠券库存" name="qty" value="10000">
                        </div>
                    </div>
                </div>
<!--                <div class="form-group" id="add_pic">-->
<!--                    <label class="col-sm-2 col-xs-12 control-label">图片</label>-->
<!--                    <div class="col-sm-8 col-xs-12">-->
<!--                        {php echo view_form_field_image('img','');}-->
<!--                        <span class="help-block">建议尺寸200*200，最好为PNG或GIF动图，可设置透明底色</span>-->
<!--                    </div>-->
<!--                </div>-->

                {if $is_presale}
<!--                <div class="form-group">-->
<!--                    <label class="col-sm-2 col-xs-12 control-label">领取身份</label>-->
<!--                    <div class="col-sm-5 col-xs-12">-->
<!--                        <select class="form-control" style="width: 140px;" name="target" id="target">-->
<!--                            {loop $select_target_arr $item}-->
<!--                            <option value="{$item['value']}" >{$item['title']}</option>-->
<!--                            {/loop}-->
<!--                        </select>-->
<!--                    </div>-->
<!--                </div>-->
                <div class="form-group">
                    <label class="col-sm-2 col-xs-12 control-label">领取方式</label>
                    <div class="col-sm-5 col-xs-12">
                        <select class="form-control" style="width: 140px;" name="get_type" id="get_type">
                            {loop $select_get_type_arr $item}
                            <option value="{$item['value']}" >{$item['title']}</option>
                            {/loop}
                        </select>
                    </div>
                </div>
                {/if}
<!--                <div class="form-group">-->
<!--                    <label class="col-sm-2 col-xs-12 control-label">领取有效期限制</label>-->
<!--                    <div class="col-sm-5 col-xs-12">-->
<!--                        <select class="form-control" style="width: 140px;" name="get_status" id="get_status">-->
<!--                            <option value="1" >不限制</option>-->
<!--                            <option value="2" >限制</option>-->
<!--                        </select>-->
<!--                    </div>-->
<!--                </div>-->

<!--                <div class="form-group" hidden="true" id="get_time">-->
<!--                    <label class="col-sm-2 col-xs-12 control-label">领取有效期</label>-->
<!--                    <div class="col-sm-8 col-xs-12">-->
<!--                        {php echo view_form_field_daterange('gettime', array('start'=>$theme_bengin_time,'end'=>$theme_end_time), true);}-->
<!--                        <span class="help-block">只在有效期内打开活动链接才会出现</span>-->
<!--                    </div>-->
<!--                </div>-->

                <div class="form-group">
                    <label class="col-sm-2 col-xs-12 control-label">使用时间</label>
                    <div class="col-sm-4 col-xs-12">
                        <select class="form-control" style="width: 140px;" name="show_type" id="show_type">
                            {loop $select_show_type_arr $item}
                            <option value="{$item['value']}" >{$item['title']}</option>
                            {/loop}
                        </select>
                    </div>
                </div>
                <div class="form-group" hidden="true" id="used_time">
                    <label class="col-sm-2 col-xs-12 control-label">使用时间设置</label>
                    <div class="col-sm-4 col-xs-12">
                        {php echo view_form_field_daterange('usedtime', array('start'=>$theme_bengin_time,'end'=>$theme_end_time), true);}
                    </div>
                </div>
                {if $is_presale}
                <div class="form-group">
                    <label class="col-sm-2 col-xs-12 control-label">使用条件</label>
                    <div class="col-sm-5 col-xs-12">
                        <select class="form-control" style="width: 140px;" name="used_type" id="used_type">
                            {loop $select_used_type_arr $item}
                            <option value="{$item['value']}" >{$item['title']}</option>
                            {/loop}
                        </select>
                    </div>
                </div>
                {/if}
                <div class="form-group">
                    <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label"><span style="color: red;">*</span>状态</label>
                    <div class="col-sm-8 col-xs-12" style="padding-top: 6px;">
                        <input  name="status" value="1" checked type="radio"> 启用
                        &nbsp;&nbsp;
                        <input  name="status" value="2"  type="radio"> 禁用
                    </div>
                </div>
                <div class="form-group" id="sub_upload1" >
                    <div class="col-sm-12">
                        <input name="submit" onclick="addItem()"  type="button" value="提交" class="btn btn-primary col-lg-2" style="float: left;    width: 200px;">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


{$pager}
<style>
    .diy_coupon_prices{
        display: flex;
    }
    .diy_coupon_span{
        width: 100px;
    }
.diy_coupon_input{
    width: 100px;
}
</style>
<script type="text/javascript">
    function submitForm(){
        var form = document.getElementById("myform");
        form.submit();
    }
    var ITEMMAX=1;
    $('#show_type').change(function () {
        var val = $("#show_type").val();
        if (val==2){
            $("#used_time").attr("hidden",false);
        }else{
            $("#used_time").attr("hidden",true);
        }
    });
    $("#get_type").change(function () {
        var val = $("#get_type").val();
        if (val==2){
            $("#used_type").val(3);
            $("#used_type").attr("disabled",'disabled');
        }else{
            $("#used_type").removeAttr("disabled");
        }
    });
    $("#get_status").change(function () {
        var val = $('#get_status').val();
        if (val==2){
            $("#get_time").show();
        }else{
            $("#get_time").hide();
        }
    });
    //跳转新增
    function jumpAdd(){
        let jump_url = "{php echo createWebUrlPlugin('coupon.add',array('themeid'=>$themeid));}";
        window.location.href = jump_url;
    }
    function closeAdd(e) {
        if (e==2){
            $("#shadow-box2").removeClass("div_flex");
            $("#shadow-box2").addClass("div_none");
        }else{
            $("#shadow-box1").removeClass("div_flex");
            $("#shadow-box1").addClass("div_none");
        }
        ITEMMAX=1;
    }
    //新增奖品 提交数据
    function addItem() {
        let postUrl = "{php echo createWebUrlPlugin('coupon.add',array('themeid'=>$themeid));}";
        let success_redirect = "{php echo createWebUrlPlugin('coupon',array('themeid'=>$themeid));}";
        let formData = $("#form1").serialize();
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    window.location.href = success_redirect;
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }
    function changeOrDelItem(e){
        let id = e.getAttribute("data-id");
        let themeid = e.getAttribute("data-themeid");
        let postUrl = e.getAttribute("data-edit-url");
        layer.msg('您确定删除吗？',{
            btn:['确定','取消'],
            yes:function (index) {
                $.ajax({
                    url:postUrl,
                    dataType:'json',
                    method:'post',
                    data: {id:id,themeid:themeid},
                    success:function (e) {
                        let res = e.result;
                        let status = res.status;
                        let msg = res.msg;
                        if (status!=1){
                            layer.msg(msg);
                        }else{
                            layer.msg(msg);
                        }
                        location.reload();
                    },
                    fail:function (err) {
                        console.log(err)
                    }
                });
                location.reload();
            }
        });
    }
    function addLtRule() {
        var item_max_id = ITEMMAX+1;
        var html = '<div class="diy_coupon_prices">\n' +
            '                                <span class="input-group-addon diy_coupon_span">满</span>\n' +
            '                                <input name="yx_lt_coupon_price_limit[]" type="text" class="form-control span3 diy_coupon_input"\n' +
            '                                       value=""/>\n' +
            '                                <span class="input-group-addon diy_coupon_span">元，减免</span>\n' +
            '                                <input name="yx_coupon_rule_price_diff[]" type="text" class="form-control span3 diy_coupon_input"\n' +
            '                                       value=""/>\n' +
            '                                <span class="input-group-addon diy_coupon_span">元</span>\n' +
            '                                <input  name="yx_coupon_price_id[]" type="hidden" value="'+item_max_id+'" />\n' +
            '                                <a href=\'javascript:;\' onclick=\'removeLtRuleItem(this)\'>\n' +
            '                                    <i class=\'glyphicon glyphicon-remove\'></i>\n' +
            '                                </a>\n' +
            '                            </div>';
        $(".yx_coupon_items").append(html);
    }
    function removeLtRuleItem(obj) {
        $(obj).parent().remove();
    }
</script>
